/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: cbus_message_packet.h
 *
 * Purpose: implementation message serialize/deserialize
 *
 * Developer:
 *   wen.gu , 2022-03-13
 *
 * TODO:
 *
 ***************************************************************************/

/******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#ifndef __CBUS_MESSAGE_PACKET_H__
#define __CBUS_MESSAGE_PACKET_H__

#include "cbus/core/cbus_type.h"
#include "cbus/core/cbus_error.h"
#include "cbus/core/cbus_buffer.h"
#include "cbus/bus/cbus_message.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/** the protocol packet type is: |start code(1byte: 0xe5)| message |end code(1byte: 0xe6)| */
#define CBUS_MSG_PKT_START_CODE ((GU08)0xe5)
#define CBUS_MSG_PKT_END_CODE ((GU08)0xe6)

/******************************************************************************
 **    TYPE DEFINITIONS
 ******************************************************************************/
G_C_HEADER_START

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

/** the callback can't do free the the instance of 'message' */
typedef void (*cbus_message_packet_deserializer_on_message_func)(cbus_message_t message);


struct _cbus_message_packet_deserializer;
typedef struct _cbus_message_packet_deserializer* cbus_message_packet_deserializer_t;
/******************************************************************************
 **    CLASSES/FUNCTIONS DEFINITIONS
 ******************************************************************************/

G_API cbus_message_packet_deserializer_t cbus_message_packet_deserializer_create(GPTR endpoint_id, cbus_message_packet_deserializer_on_message_func on_message, GPTR opaque);
G_API void cbus_message_packet_deserializer_destroy(cbus_message_packet_deserializer_t parser);


G_API GErrc cbus_message_packet_deserializer_deserialize(cbus_message_packet_deserializer_t parser, const GBUF buf, GU32 size);

G_API void cbus_message_packet_deserializer_check_valid(cbus_message_packet_deserializer_t parser, const char* check_at, int check_line);

//G_API cbus_buffer_t cbus_message_packet_serializer_serialize(const cbus_message_t* message);

G_C_HEADER_END
#endif /** !__CBUS_MESSAGE_PACKET_H__ */

